package com.bamboo.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.bamboo.eduservice.entity.EduSubject;
import com.bamboo.eduservice.entity.SubjectData;
import com.bamboo.eduservice.entity.subject.OneSubject;
import com.bamboo.eduservice.entity.subject.TwoSubject;
import com.bamboo.eduservice.listener.SubjectListener;
import com.bamboo.eduservice.mapper.EduSubjectMapper;
import com.bamboo.eduservice.service.EduSubjectService;
import com.bamboo.servicebase.exceptionhandler.BambooException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author jack.wu
 * @since 2020-07-04
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSubject(MultipartFile file, EduSubjectService subjectService) {
        try {
            InputStream in = file.getInputStream();
            EasyExcel.read(in, SubjectData.class, new SubjectListener(subjectService)).sheet().doRead();
        } catch (Exception e) {
            throw new BambooException();
        }
    }

    @Override
    public EduSubject existOneSubject(String title) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title", title);
        wrapper.eq("parent_id", "0");
        return getOne(wrapper);
    }

    @Override
    public EduSubject existTwoSubject(String title, String pid) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title", title);
        wrapper.eq("parent_id", pid);
        return getOne(wrapper);
    }

    @Override
    public List<OneSubject> getSubjectList() {
        List<OneSubject> result = new ArrayList<>(10);
        // 查询所有一级分类
        QueryWrapper<EduSubject> oneWrapper = new QueryWrapper<>();
        oneWrapper.eq("parent_id", "0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(oneWrapper);
        // 查询所有二级分类
        QueryWrapper<EduSubject> twoWrapper = new QueryWrapper<>();
        twoWrapper.ne("parent_id", "0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(twoWrapper);

        for (EduSubject subject : oneSubjectList) {
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(subject, oneSubject);
            result.add(oneSubject);

            List<TwoSubject> twoSubjects = new ArrayList<>(10);
            for (EduSubject two : twoSubjectList) {
                if (subject.getId().equals(two.getParentId())) {
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(two, twoSubject);
                    twoSubjects.add(twoSubject);
                }
            }
            oneSubject.setChildren(twoSubjects);
        }
        return result;
    }
}
